﻿using Kimd.Communication;
using Kimd.Machine;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WorkStation.Common;

namespace WorkStation.UI.Controls
{
    public partial class DeviceModule : UserControl
    {
        private ICommunicationShow _communicationOperator;

        private string _description;
        public string Description
        {
            get { return _description; }
            set 
            { 
                _description = value;
                this.l_Title.Text = _description;
            }
        }
        private Color _selectedColor;
        public Color SelectedColor
        {
            get { return _selectedColor; }
            set 
            { 
                _selectedColor = value;
                this.BackColor = _selectedColor;
            }
        }
        private string _status;
        public string Status
        {
            get 
            { 
                return _status; 
            }
            set 
            { 
                _status = value;
                if (string.IsNullOrEmpty(_status))
                    return;
                if (_status.Equals("已连接"))
                    l_Status.Image = Properties.Resources.ComStatusOK;
                else if (_status.Equals("未连接"))
                    l_Status.Image = Properties.Resources.ComStatusNG;
                else if (_status.Equals("连接中..."))
                    l_Status.Image = Properties.Resources.ComStatusIdle;
                l_Status.Text = _status;
            }
        }
        private string _info;
        public string Info
        {
            get { return _info; }
            set 
            { 
                _info = value;
                l_Message.Text = _info;
            }
        }
        public event EventHandler OnDeviceSelected;
        public Action<object, string> OnStatusChanged;
        public DeviceModule()
        {
            InitializeComponent();
        }
        public Form GetDeviceForm()
        {
            if(_communicationOperator == null)
                return null;
            Form deviceForm = null;
            deviceForm = _communicationOperator.GetDebugForm();
            return deviceForm;
        }
        public void SetOperator(ICommunicationShow comOperator)
        {
            _communicationOperator = comOperator;
        }
        public ICommunicationShow GetOperator()
        {
            return _communicationOperator;
        }
        private void module_MouseDown(object sender, MouseEventArgs e)
        {
            DeviceModule_MouseDown(sender, e);
        }

        private void DeviceModule_MouseDown(object sender, MouseEventArgs e)
        {
            this.SelectedColor = Color.Yellow;
        }

        private void DeviceModule_MouseUp(object sender, MouseEventArgs e)
        {
            this.SelectedColor = Color.White;
        }

        private void module_MouseUp(object sender, MouseEventArgs e)
        {
            DeviceModule_MouseUp(sender, e);
        }

        private void module_Click(object sender, EventArgs e)
        {
            DeviceModule_Click(sender, e);  
        }

        private void DeviceModule_Click(object sender, EventArgs e)
        {
            if (OnDeviceSelected != null)
                OnDeviceSelected(this, e);
        }

        private async void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if(_communicationOperator == null)
            {
                if (OnStatusChanged != null)
                    OnStatusChanged(this, "尚未配置通信接口!");
                return;
            }
            if (_communicationOperator.IsOpenFlag)
                return;
            this.Status = "连接中...";
            打开ToolStripMenuItem.Enabled = false;
            bool result = await Task.Run(() =>
            {
                _communicationOperator.SetOpenOrClose(OpenOrClose.Open);
                return _communicationOperator.IsOpenFlag;
            });
            this.Status = result ? "已连接" : "未连接";
            if (this.Status.Equals("未连接"))
            {
                if (OnStatusChanged != null)
                    OnStatusChanged(this, $"{this.Name} 连接失败!");
            }
            打开ToolStripMenuItem.Enabled = true;
        }

        private void 关闭ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            _communicationOperator.SetOpenOrClose(OpenOrClose.Close);
            this.Status = _communicationOperator.IsOpenFlag ? "已连接" : "未连接";
            //if (OnStatusChanged != null)
            //    OnStatusChanged(this, e);
        }

        private void 设置ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormCommunication2 commForm = new FormCommunication2(_communicationOperator);
            commForm.StartPosition = FormStartPosition.CenterScreen;
            if(commForm.ShowDialog() == DialogResult.OK)
            {
                MachineResource.Instance.Save(Properties.Settings.Default.ConfigName);
            }
        }
    }
}
